package com.jz.banner.screen.ui.service;

import android.content.Context
import cn.jpush.android.api.CustomMessage
import cn.jpush.android.service.JPushMessageReceiver
import com.jz.banner.screen.bean.ExtrasBean
import com.jz.banner.screen.bean.PropellingMessage
import com.jz.banner.screen.utils.CommUtils
import com.jz.banner.screen.utils.eventbus.Event
import com.jz.banner.screen.utils.eventbus.EventBusUtil
import com.jz.banner.screen.utils.eventbus.EventCode


class MyJPushReceiver : JPushMessageReceiver() {
    override fun onMessage(context: Context, customMessage: CustomMessage) {
        //super.onMessage(context, customMessage)
        CommUtils.println("JPushInterface.getAlias:", "${CommUtils.gson.toJson(customMessage)},ThreadName:${Thread.currentThread().name}")
        var extrasBean: ExtrasBean = try {
            CommUtils.gson.fromJson(customMessage.extra, ExtrasBean::class.java)
        } catch (e: Exception) {
            //println("JPushInterface.getAlias:", "extrasBean Error:$e")
            ExtrasBean()
        }
        //println("JPushInterface.getAlias:", "extrasBean:${Gson().toJson(extrasBean)}")
        //支付语音播报
        when (extrasBean.type) {
            1 -> {
                if (extrasBean.sound_play) EventBusUtil.sendEvent(Event(EventCode.VOICE_MSG, PropellingMessage(customMessage.title, customMessage.message, customMessage.extra)))
            }
        }
    }
}